package psrc.logic.pages.fabrica;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import psrc.logic.pages.base.ControllerBase;
import psrc.logic.util.beans.FabricaBean;

public class SendFabricaRemoveController extends ControllerBase{

	private static final long serialVersionUID = 1L;
	String[] fabricaCheckbox;

	@Override
	public String executeController() throws Exception {
		FabricaModel model = new FabricaModel();
		List<FabricaBean> todosFabricas = model.getFabricasList();
		List<FabricaBean> fabricasParaRemover = new ArrayList<FabricaBean>();
		List<String> idsParaRemover = Arrays.asList(getFabricaCheckbox());

		for (FabricaBean prod : todosFabricas) {
			if (idsParaRemover.contains(prod.getIdFabrica())) {
				fabricasParaRemover.add(prod);
			}
		}
		try {
			if (model.removeFabrica(fabricasParaRemover)) {
				return SUCCESS;
			}
		}  catch (Exception e) {
			addActionError(getText("error.constraint"));
		}
		return ERROR;
	}

	/**
	 * @return the fabricaCheckbox
	 */
	public String[] getFabricaCheckbox() {
		return fabricaCheckbox;
	}

	/**
	 * @param fabricaCheckbox
	 *            the fabricaCheckbox to set
	 */
	public void setFabricaCheckbox(String[] fabricaCheckbox) {
		this.fabricaCheckbox = fabricaCheckbox;
	}


}
